<template>
  <p>
    <a-space>
      <!-- 车次选择下拉框-->
      <train-select v-model="queryTrainCode" width="200px"/>
      <a-button type="primary" @click="handleQuery()">查找</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
  </p>
  <!--表格-->
  <a-table :dataSource="trainCarriages"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
      </template>
      <template v-else-if="column.dataIndex === 'seatType'">
        <span v-for="item in SEAT_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.seatType">
            {{item.desc}}
          </span>
        </span>
      </template>
    </template>
  </a-table>
  <!--添加表单-->
  <a-modal v-model:visible="visible" title="车厢" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="trainCarriage" :label-col="{span: 5}" :wrapper-col="{ span: 20 }">
      <a-form-item label="车次编号">
        <!-- 车次选择下拉框-->
        <train-select v-model="trainCarriage.trainCode"/>
      </a-form-item>
      <a-form-item label="厢号">
        <a-input v-model:value="trainCarriage.index" />
      </a-form-item>
      <a-form-item label="座位类型">
        <a-select v-model:value="trainCarriage.seatType">
          <a-select-option v-for="item in SEAT_TYPE_ARRAY" :key="item.code" :value="item.code">
            {{item.desc}}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="排数">
        <a-input v-model:value="trainCarriage.rowCount" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import {onMounted, ref} from 'vue';
import request from "@/utils/request";
import {notification} from 'ant-design-vue'
import TrainSelect from "@/components/TrainSelect.vue";

// 火车车厢枚举
const SEAT_TYPE_ARRAY = window.SEAT_TYPE_ARRAY;
// 新增按钮隐藏属性
const visible = ref(false);
// 表格加载
let loading = ref(false);
// 添加火车车厢的属性
let trainCarriage = ref({
  id: undefined,
  trainCode: undefined,
  index: undefined,
  seatType: undefined,
  seatCount: undefined,
  rowCount: undefined,
  colCount: undefined
});
// 表格数据
const trainCarriages = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  position: ['bottomCenter'],
  total: 0,
  current: 1,
  pageSize: 8,
  showSizeChanger: true,
  pageSizeOptions: ['8', '15', '20']
});
// 表格展示列
const columns = [
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
  },
  {
    title: '厢号',
    dataIndex: 'index',
    key: 'index',
  },
  {
    title: '座位类型',
    dataIndex: 'seatType',
    key: 'seatType',
  },
  {
    title: '座位数',
    dataIndex: 'seatCount',
    key: 'seatCount',
  },
  {
    title: '排数',
    dataIndex: 'rowCount',
    key: 'rowCount',
  },
  {
    title: '列数',
    dataIndex: 'colCount',
    key: 'colCount',
  },
  {
    title: '操作',
    dataIndex: 'operation'
  }
];

// 点击添加按钮，表单显示
const onAdd = () => {
  trainCarriage.value = {};
  visible.value = true;
};

// 编辑火车车厢
const onEdit = (record) => {
  trainCarriage.value = window.Tool.copy(record);
  visible.value = true;
};

// 删除火车车厢
const onDelete = (record) => {
  request.delete(`/business/admin/train-carriage/delete/${record.id}`)
    .then((result) => {
      if (result.status) {
        notification.success({description: result.message});
        handleQuery({
          pageNum: pagination.value.current,
          pageSize: pagination.value.pageSize,
        });
      } else {
        notification.error({description: result.message});
      }
  });
};

// 请求 -- 保存火车车厢
const handleOk = () => {
  request.post("/business/admin/train-carriage/save", trainCarriage.value)
    .then((result) => {
      if (result.status) {
      notification.success({description: result.message});
      visible.value = false;
      handleQuery({
        pageNum: pagination.value.current,
        pageSize: pagination.value.pageSize
      });
    } else {
      notification.error({description: result.message});
    }
  });
};

// 查询车次编号
let queryTrainCode = ref(null);
// 请求 -- 查询火车车厢
const handleQuery = (param) => {
  // 如果无参，则查询当前页
  if (!param) {
    param = {
      pageNum: pagination.value.current,
      pageSize: pagination.value.pageSize
    }
  }
  loading.value = true;
  request.get("/business/admin/train-carriage/list", {
    params: {
      pageNum: param.pageNum,
      pageSize: param.pageSize,
      trainCode: queryTrainCode.value
    }
  }).then((result) => {
    loading.value = false;
    if (result.status) {
      trainCarriages.value = result.content.records;
      // 设置分页控件的值
      pagination.value.current = param.pageNum;
      pagination.value.total = result.content.total;
    } else {
      notification.error({description: result.message});
    }
  });
};

// 表格换页，调用请求
const handleTableChange = (page) => {
  pagination.value = page
  handleQuery({
    pageNum: page.current,
    pageSize: page.pageSize,
  });
};

onMounted(() => {
  handleQuery({
    pageNum: 1,
    pageSize: pagination.value.pageSize
  });
});
</script>
